﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using FUSE.AzureConfig.Tasks;
using System.Collections.Generic;

namespace AzureConfGen.Tests
{
    [TestClass]
    public class InheritanceTests
    {
        [TestMethod]
        public void SimpleInheritanceWorks()
        {
            var confStr = Properties.Resources.ConfWithInheritance;
            var conf = JsonConvert.DeserializeObject<AzureConfigSpec>(confStr);
            Assert.AreEqual(2, conf.Environments.Count);
            EnvironmentSpec env;
            Assert.IsTrue(conf.Environments.TryGetValue("basic", out env));
            Assert.AreEqual("common", env.Parent);
        }

        [TestMethod]
        public void InheritanceRemovesNonDeployedParents()
        {
            var confStr = Properties.Resources.ConfWithInheritance;
            var conf = JsonConvert.DeserializeObject<AzureConfigSpec>(confStr);
            var processed = new SpecProcessor().Process(conf);
            Assert.AreEqual(1, processed.Environments.Count);
            Assert.AreEqual("Foo", processed.Environments.Keys.First());
        }

        [TestMethod]
        public void InheritanceCopiesSettings()
        {
            var confStr = Properties.Resources.ConfWithInheritance;
            var conf = JsonConvert.DeserializeObject<AzureConfigSpec>(confStr);
            var processed = new SpecProcessor().Process(conf);
            var env = processed.Environments["Foo"];
            Assert.AreEqual("common value", env.Roles["WebRole"].ConfigSettings.Settings["common_key"]);
        }

        [TestMethod]
        public void ChildSettingsClobberParent()
        {
            var confStr = Properties.Resources.ConfWithInheritance;
            var conf = JsonConvert.DeserializeObject<AzureConfigSpec>(confStr);
            var processed = new SpecProcessor().Process(conf);
            var env = processed.Environments["Foo"];
            Assert.AreEqual("val1", env.Roles["WebRole"].ConfigSettings.Settings["key1"]);
        }

        [TestMethod]
        public void InheritanceCopiesCertificates()
        {
            var confStr = Properties.Resources.ConfWithInheritance;
            var conf = JsonConvert.DeserializeObject<AzureConfigSpec>(confStr);
            var processed = new SpecProcessor().Process(conf);
            var env = processed.Environments["Foo"];
            var cert = env.Roles["WebRole"].Certificates["Microsoft.WindowsAzure.Plugins.RemoteAccess.PasswordEncryption"];
            Assert.IsNotNull(cert);
            Assert.AreEqual("fakeguidvalue", cert.Thumbprint);
        }

        [TestMethod]
        public void ChildCertificatesClobberParent()
        {
            var confStr = Properties.Resources.ConfWithInheritance;
            var conf = JsonConvert.DeserializeObject<AzureConfigSpec>(confStr);
            var processed = new SpecProcessor().Process(conf);
            var env = processed.Environments["Foo"];
            var cert = env.Roles["WebRole"].Certificates["Cert2"];
            Assert.IsNotNull(cert);
            Assert.AreEqual("fakeguidvalue2_override", cert.Thumbprint);
            Assert.AreEqual("sha1", cert.ThumbprintAlgorithm);
        }
    }
}
